package settings

import (
	"black_mamba/global"
	"black_mamba/pages"
	"crypto/md5"
	_ "embed"
	"fmt"
	"gitee.com/wengo/windows_gui/widget"
	"gitee.com/wengo/windows_gui/xc"
	"gitee.com/wengo/windows_gui/xcc"
	"strings"
)

type BasicPage struct {
	pages.Foundation
	style            map[string]int
	serialNumberEdit *widget.Edit
	saveBasicBtn     *widget.Button
}

func NewBasicPage() *BasicPage {
	basicPage := &BasicPage{
		Foundation: pages.Foundation{
			ExecuteBtn: nil,
			LogEdit:    nil,
			Style:      nil,
		},
		serialNumberEdit: widget.NewEditByName("serialNumberEdit"),
		saveBasicBtn:     widget.NewButtonByName("saveBasicBtn"),
	}

	// 绑定执行按钮事件
	basicPage.saveBasicBtn.Event_BnClick(basicPage.save)

	// 初始化数据
	//BasicPage.InitBasicType()
	basicPage.GetBasic()

	return basicPage
}

/* 事件回调 */
func (b *BasicPage) save(pbHandled *bool) int {
	var serialNumber string

	//var BasicConfig = BasicConfig{}
	b.serialNumberEdit.GetText(&serialNumber, 1024)
	serialNumber = strings.TrimSpace(serialNumber)
	global.BM_CONFIG.Section("basic").Key("serialNumberCiphertext").Value()
	_, err := global.BM_CONFIG.Section("basic").NewKey("serialNumberPlaintext", serialNumber)

	re := md5.Sum([]byte(serialNumber))
	_, err = global.BM_CONFIG.Section("basic").NewKey("serialNumberCiphertext", fmt.Sprintf(`%x`, re))

	if err != nil {
		xc.XNotifyMsg_WindowPopup(global.BM_WINDOW.Handle, xcc.Position_Flag_Bottom, "系统提示: ", "保存失败", 0, xcc.NotifyMsg_Skin_Error)
	} else {
		global.BM_CONFIG.SaveTo("config.ini")
		xc.XNotifyMsg_WindowPopup(global.BM_WINDOW.Handle, xcc.Position_Flag_Bottom, "系统提示: ", "保存成功", 0, xcc.NotifyMsg_Skin_Success)
	}

	return 0
}

//func (a *BasicPage) InitBasicType() {
//	a.BasicTypeCombo.CreateAdapter()
//	a.BasicTypeCombo.AddItemText("宽带连接")
//	a.BasicTypeCombo.AddItemText("隧道代理")
//}
//

func (b *BasicPage) GetBasic() {
	serialNumberPlaintext := global.BM_CONFIG.Section("basic").Key("serialNumberPlaintext").Value()

	b.serialNumberEdit.SetText(serialNumberPlaintext)
}
